(function($){
$.fn.extend({
	// 扩展 jQuery 元素集来提供新的方法。
	buildThread : function (){
		return $(this).filter('[url]').each(function() {
			var $container = $(this), url = $container.attr("url");
			$container.load(url + ".action", function(){
				$grid = $container.find("table");
				$grid.find("td[state]").each(function(){
					var $td = $(this), state = $td.html(), img = "";
					if (state == "NEW") {
						// img = "";
					}
					else if (state == "RUNNABLE") {
						img = "green.png";
					}
					else if (state == "BLOCKED") {
						img = "blue.png";
					}
					else if (state == "WAITING") {
						img = "yellow.png";
					}
					else if (state == "TIMED_WAITING") {
						img = "yellow.png";
					}
					else if (state == "TERMINATED") {
						// img = "";
					}
					if("" != img) {
						$td.prepend("<img src='resources/xframe/monitor/bullets/"+img+"' title='"+state+"'>");
					}
				});
				
				$grid.find("a[tid]").click(function(){
					if(!confirm("杀掉一个Java线程是不推荐和不安全的，你确定要杀掉？")) return;
					var tid = $(this).attr("tid");
					$.post(url + "!interrupt.action", {id: tid}, function(){ alert("已受理。");});
				}).each(function(){
					var $a = $(this), name = $a.html();
					$a.html("<img width='16' height='16' src='resources/xframe/monitor/stop.png' title='杀掉线程&nbsp;" + name + "'>");
				});
				
				// $("*[title]").attr("alt", function(){ return this.title; });
				
				$grid.find("tbody > tr:odd").addClass("odd");
				$grid.find("tbody > tr").hover(function(){ $(this).toggleClass("highlight"); });
			});
		});
	},
	buildMemory : function (){
		return $(this).filter('[url]').each(function() {
			var $container = $(this), url = $container.attr("url");
			$container.load(url + ".action", function(){
				// $grid = $container.find("table");
			});
		});
	}
});
})(jQuery);

// 自动更新的事务ID。
var Memory_Refresh_Interval_ID, 
	Thread_Refresh_Interval_ID;
 // 自动更新间隔（毫秒）。
var Auto_Refresh_MS = 1500;

jQuery(function($){	
	// 内存……
	var $memory_container = $("#memory_container").buildMemory();
	var $a_auto_refresh_memory = $("#a_auto_refresh_memory");
	$a_auto_refresh_memory.toggle(function(){
		$a_auto_refresh_memory.html("关闭自动更新");
		Memory_Refresh_Interval_ID = window.setInterval(function(){ $memory_container.buildMemory(); }, Auto_Refresh_MS);
	},function(){
		$a_auto_refresh_memory.html("自动更新？");
		window.clearInterval(Memory_Refresh_Interval_ID);
	});
	
	// 线程……
	var $thread_container = $("#thread_container").buildThread();
	var $a_auto_refresh_thread = $("#a_auto_refresh_thread");
	$a_auto_refresh_thread.toggle(function(){
		$a_auto_refresh_thread.html("关闭自动更新");
		Thread_Refresh_Interval_ID = window.setInterval(function(){ $thread_container.buildThread(); }, Auto_Refresh_MS);
	},function(){
		$a_auto_refresh_thread.html("自动更新？");
		window.clearInterval(Thread_Refresh_Interval_ID);
	});
	
});